package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.MompSysParamMapper;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 系统参数表接口类 营销用
 *
 * @author liujinan
 * @version 1.0.0
 * @date 2020/6/23
 */
public class MompSysParamMapperImpl implements MompSysParamMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    @Override
    public int insertSysParam(SysParam sysParam) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "momoInsertSysParam");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, sysParam, null);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("登记系统参数表失败,instId:<{}>,paramType:<{}>,paramKey:<{}>,iReturnCode:<{}>", sysParam.getInstId(), sysParam.getParamType(), sysParam.getParamKey(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int deletePointRuleDataInfo(String instId, String paramType, String paramKey) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompDeleteSysParamByPrimaryKey");

        SysParam sysParam = new SysParam();
        sysParam.setInstId(instId);
        sysParam.setParamKey(paramKey);
        sysParam.setParamType(paramType);

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, sysParam, SysParam.class);
        if (Database.DBS_SUCCESS != iReturnCode && Database.DBS_NOT_FOUND != iReturnCode) {
            rglog.error("删除系统参数表失败,instId:<{}>,paramType:<{}>,paramKey:<{}>,iReturnCode:<{}>", sysParam.getInstId(), sysParam.getParamType(), sysParam.getParamKey(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public SysParam getSysParamInfoBuyPrimaryKey(String instId, String paramType, String paramKey) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompSelectSysparamByPrimaryKey");
        SysParam sysParam = new SysParam();
        sysParam.setInstId(instId);
        sysParam.setParamType(paramType);
        sysParam.setParamKey(paramKey);
        int recode = dbsUtil.dbsExecuteSql(sqlStr, sysParam, sysParam);

        if (Database.DBS_NOT_FOUND == recode) {
            rglog.info("系统参数信息查询无记录，instId:<{}>,paramType:<{}>,paramKey:<{}>", instId, paramType, paramKey);
            return null;
        } else if (Database.DBS_SUCCESS != recode) {
            rglog.info("系统参数信息查询无记录，instId:<{}>,paramType:<{}>,paramKey:<{}>", instId, paramType, paramKey);
            return null;
        } else {
        }
        return sysParam;
    }
}
